package com.jtcoding.chat.controller;

import com.jtcoding.chat.common.enums.ResultCode;
import com.jtcoding.chat.exception.ApiException;
import com.jtcoding.chat.model.*;
import com.jtcoding.chat.service.UserService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

/**
 * @author jason.tang
 * @create 2018-12-27 12:52
 * @description
 */

@Api(tags = "用户相关")
@RestController
@RequestMapping(value = "/users")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation(value = "用户登录", notes = "用户登录")
    @PostMapping("/login")
    public Result<User> userLogin(@RequestBody User user) {
        return Result.success(userService.userLogin(user));
    }

    @ApiOperation(value = "用户注册", notes = "用户注册")
    @PostMapping("/register")
    public Result<User> userRegister(@RequestBody User user) throws Exception {
        return Result.success(userService.addUser(user));
    }

    @ApiOperation(value = "用户激活", notes = "用户激活")
    @GetMapping("/activation/{userNum}")
    @ApiImplicitParam(name = "userNum", value = "用户编号", required = true, dataType = "int", paramType = "path")
    public void activationUser(@PathVariable Integer userNum, HttpServletResponse response) throws IOException {
        Boolean activationUser = userService.activationUser(userNum);
        response.setContentType("text/html; charset=utf-8");
        PrintWriter writer = response.getWriter();
        if (activationUser) {
            writer.write("<lable>激活成功，祝您使用愉快！</lable>");
        } else {
            writer.write("<lable>激活失败，请重新激活！</lable>");
        }
    }

    @ApiOperation(value = "更新头像", notes = "更新头像")
    @PostMapping("/portrait/{userNum}")
    @ApiImplicitParam(name = "userNum", value = "用户编号", required = true, dataType = "int", paramType = "path")
    public Result<Map<String, Object>> modifyUserPortrait(@PathVariable Integer userNum, @RequestParam("file") MultipartFile file) throws Exception {
        if (file == null) {
            throw new ApiException(ResultCode.FILE_DOES_NOT_EXIST);
        }
        return Result.success(userService.modifyUserPortrait(userNum, file));
    }

    @ApiOperation(value = "修改用户名", notes = "修改用户名")
    @PutMapping("/username")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userNum", value = "用户编号", required = true, dataType = "int"),
            @ApiImplicitParam(name = "username", value = "新用户名", required = true, dataType = "String")
    })
    public Result<Map<String, Object>> modifyUsername(@RequestParam("userNum") Integer userNum, @RequestParam("username") String username) {
        User user = User.builder().userNum(userNum).username(username).build();
        return Result.success(userService.modifyUsername(user));
    }

    @ApiOperation(value = "修改个性签名", notes = "修改个性签名")
    @PutMapping("/signature/{userNum}")
    public Result<Map<String, Object>> modifySignature(@PathVariable Integer userNum, @RequestBody User user) {
        user.setUserNum(userNum);
        return Result.success(userService.modifySignature(user));
    }

    @ApiOperation(value = "刷新二维码", notes = "刷新二维码")
    @GetMapping("/qrcode/{userNum}")
    @ApiImplicitParam(name = "userNum", value = "用户编号", required = true, dataType = "int", paramType = "path")
    public Result<Map<String, Object>> modifyQRCode(@PathVariable Integer userNum) throws Exception {
        return Result.success(userService.modifyQRCode(userNum));
    }

    @ApiOperation(value = "根据编号获取User信息", notes = "根据编号获取User信息")
    @GetMapping("/{userNum}")
    @ApiImplicitParam(name = "userNum", value = "用户编号", required = true, dataType = "int", paramType = "path")
    public Result<User> getUserByNum(@PathVariable Integer userNum) {

        return Result.success(userService.getUserByNum(userNum));
    }
}